package com.afkt.project.ui.slice;
import com.afkt.project.model.bean.CommodityBean;

import com.afkt.project.ui.widget.swipeaction.Contact;
import com.afkt.project.ui.widget.swipeaction.ContactAdapter;
import com.afkt.project.ui.widget.swipeaction.SwipeDirection;
import com.ohos.afkt.ResourceTable;
import com.afkt.project.base.app.BaseActivity;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.window.dialog.ToastDialog;
import java.util.ArrayList;

import static com.afkt.project.ui.widget.swipeaction.SwipeDirection.*;


public class SwipeActionSlice  extends BaseActivity implements ContactAdapter.ContactCallbacks{
    /**
     * 标题栏
     */
    private DirectionalLayout bar_layout;
    private DirectionalLayout bar;
    private Image slider_return;
    private ContactAdapter listItemProvider;
    private ListContainer listContainer;
    private ArrayList<Contact> contacts = new ArrayList<>();
    @Override
    protected int baseLayoutId() {
        return ResourceTable.Layout_base_view_listContainer_slider;
    }

    @Override
    protected void initView() {
        bar_layout = (DirectionalLayout) findComponentById(ResourceTable.Id_slider_bar_layout);
        bar = (DirectionalLayout) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_recycle_slider_bar, null, false);
        bar_layout.addComponent(bar);
        slider_return = (Image) bar.findComponentById(ResourceTable.Id_silder_return);

        listContainer = (ListContainer) findComponentById(ResourceTable.Id_slider_list);
        contacts.addAll(CommodityBean.getEnglishContacts());
        listItemProvider = new ContactAdapter(SwipeActionSlice.this.getAbility(), contacts,ResourceTable.Layout_row_bg,listContainer,this);
        listContainer.setItemProvider(listItemProvider);

        listItemProvider.addBackground(SwipeDirection.DIRECTION_FAR_LEFT,new RgbColor(255, 0, 255))
                .addBackground(DIRECTION_NORMAL_LEFT, new RgbColor(255, 255, 0))
                .addBackground(DIRECTION_FAR_RIGHT, new RgbColor(0, 255, 255))
                .addBackground(DIRECTION_NORMAL_RIGHT,new RgbColor(255, 0, 0));
    }

    @Override
    protected void initListener() {
        slider_return.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void onBackground() {
        bar_layout.removeComponent(bar);
        super.onBackground();
    }

    @Override
    public boolean hasActions(int i, SwipeDirection swipeDirection) {
        if(swipeDirection.isLeft()){
            return false;
        }
        if(swipeDirection.isRight()) {
            return true;
        }
        return false;
    }

    @Override
    public boolean shouldDismiss(int i, SwipeDirection swipeDirection) {
        return swipeDirection == DIRECTION_NORMAL_LEFT;
    }

    @Override
    public void onSwipe(int direction, SwipeDirection swipeDirection) {
        String dir = "";
        switch (direction) {
            case 1:
                dir = "Far left";
                break;
            case 0:
                dir = "Left";
                break;
            case 3:
                dir = "Far right";
                break;
            case 2:
                dir = "Right";
                break;
        }
        ToastDialog builder = new ToastDialog(this);
        builder.setText(dir).show();
        listItemProvider.notifyDataChanged();
    }
}
